$.fn.adTip = function(options, param) {

    let self = this;

    if (typeof options === "string") {
        return $.fn.adTip.methods[options](self, param);
    }

    options = $.extend({}, $.fn.adTip.defaults, options || {});

    let pic = "";
    let $lis = [];
    $.each(options.data, function(index, item) {

        if (options.lastPic && index == options.data.length - 1 && item.indexOf("/Hotel/evalWord") != -1) {
            pic = item;
        } else {
            let $li = $.fn.adTip.methods["createTip"](self, {
                value: item,
                add: index == options.data.length - 1
            });
            $lis.push($li);
        }

    });

    if (options.data.length == 0) {
        let $li = $.fn.adTip.methods["createTip"](self, {
            value: "",
            add: true
        });
        $lis.push($li);
    }

    this.empty().append($lis);

    let uploader = null;

    if (options.lastPic) {
        let $li = $("<li id='evalWord-fix'></li>");
        uploader = $li.adFile({
            img: pic,
            groupId: options.groupId,
            module: "evalWord",
            removeCallback: options.removeCallback
        });
        self.append($li);
    }

    self.on("click", ".icon-add", function() {
        $(this).parent().remove();
        let $li = $.fn.adTip.methods["createTip"](self, {
            value: "",
            add: true
        });
        if (options.lastPic) {
            self.find("#evalWord-fix").before($li);
        } else {
            self.append($li);
        }
    });

    self.on("click", ".icon-del", function() {
        let $li = $(this).parent().parent();
        if ($li.find(".icon-add").length != 0) {
            $li.prev().append($('<span><i class="icon icon-add"></i><span>'));
        }
        $li.remove();
    });

    if (uploader != null) {
      return uploader;
    }
}

$.fn.adTip.methods = {
    getTips: function(ele) {
        let tips = [];
        ele.find("input[type='text']").each(function(index, item) {
            let val = $(item).val().trim();
            if (val != "") {
                tips.push(val);
            }
        });
        return tips;
    },
    createTip: function(ele, param) {
        let $li = $(`<li><input type="text" name="" class="text" value="` + param.value + `"></li>`);

        if (ele.find("li").length != 0) {
            $li.append($('<span><i class="icon icon-del"></i><span>'));
        }

        if (param.add) {
            $li.append($('<span><i class="icon icon-add"></i><span>'));
        }

        ele.find("li").each(function(index, item) {
            if ($(item).find(".icon-del").length == 0) {
                $(item).append($('<span><i class="icon icon-del"></i><span>'));
            }
        });
        return $li;
    }
}
